var moduleUrl = "/furerp/workshop/";
var baseUrl;

function bodyOnload() {
    baseUrl = document.getElementById("baseUrl").value;
    var xhr = new XMLHttpRequest();
    xhr.open("GET", baseUrl + moduleUrl + "getWorkshopJSONObject");
    xhr.setRequestHeader('Content-Type','application/json;charset=utf-8');
    xhr.onreadystatechange = function () {
        if(XMLHttpRequest.DONE === xhr.readyState){
            if(200 === xhr.status){
                if(null == xhr.responseText){
                    return;
                }
                var resultJson = eval("(" + xhr.responseText + ")");
                showTable(resultJson);
            }
        }
    }
    xhr.send();
}

function showTable(joRe) {
    var msg = document.getElementById("message");
    var divTable = document.getElementById("divTable");
    var exportContent = joRe["exportContent"];
    if(null == exportContent || 0 == exportContent.length){
        msg.innerText = "无部门记录";
        divTable.style.display = "none";
        return;
    }
    else {
        filTable(joRe.exportField, joRe.exportContent);
        divTable.style.display = "block";
    }
}

const tableId = "tbDetail";

/*
 * 填充表格 exportField 字段对象(保存代码,名称(数组)) exportContent 表格内容对象(数组)
 */
function filTable(exportField, exportContent) {

    var tbdetail = document.getElementById(tableId);
    tbdetail.innerHTML = "";

    var row = document.createElement("tr");
    var fieldName;
    for (var i = 0; i < exportField.length; ++i) {
        // 添加表头
        fieldName = exportField[i][1];
        addTh(row, fieldName);
    }
    addTd(row, "管理");
    tbdetail.appendChild(row);

    for (var i = 0; i < exportContent.length; i++) {
        // 循环添加行
        var jsonObj = exportContent[i];
        row = document.createElement("tr");
        for (var col = 0; col < exportField.length; ++col) {
            // 将其中每个对象的属性取出添加列
            addTd(row, jsonObj[exportField[col][0]]); // 按字段名取内容
        }
        var delink = document.createElement("a");
        delink.href = baseUrl + "/furerp/workshop/delete?wsId=" + jsonObj["wsId"];
        delink.innerText = "删除";
        addTdLink(row, delink);
        tbdetail.appendChild(row);
    }

}

function addTh(row, str) {
    var tnode = document.createTextNode(str);
    var th = document.createElement("th");
    th.setAttribute("class", "tableTitle");
    th.appendChild(tnode);
    row.appendChild(th);
}
function addTd(row, str) {
    var tnode = document.createTextNode(str);
    var td = document.createElement("td");
    td.appendChild(tnode);
    row.appendChild(td);
}

function addTdLink(row, link) {
    var td = document.createElement("td");
    td.appendChild(link);
    row.appendChild(td);
}